package com.gitee.wsl.struct.tree.sample.trie

class TrieNode<V> {
    /**
     * 如果不为null，则表示匹配了完整路径
     */
    var value: V? = null

    /**
     * 此配置仅在value不为null时有效。它表示匹配所有后续路径
     */
    var isTillEnd: Boolean = false

    private val children: MutableMap<String, TrieNode<V>> = HashMap<String, TrieNode<V>>()

    var wildcardChild: TrieNode<V>? = null

    fun hasChild(): Boolean {
        return wildcardChild != null || children.isNotEmpty()
    }

    fun getExactMatchChild(name: String): TrieNode<V>? {
        return children[name]
    }

    fun makeExactMatchChild(name: String): TrieNode<V> {
        var child = children[name]
        if (child == null) {
            child = TrieNode()
            children[name] = child
        }
        return child
    }

    fun getChildren(): Map<String, TrieNode<V>> {
        return children
    }
}